﻿package component.fileupload.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import component.fileupload.action.FileDownloadAction;
import component.fileupload.action.FileUploadActionFactory;
import component.fileupload.action.IFileDownloadAction;
import component.fileupload.action.IFileUploadAction;
import component.fileupload.exception.FileUploadException;


public class UploadAction extends Action{
	
	public ActionForward execute(ActionMapping actionmapping,
			ActionForm actionform, HttpServletRequest request,
			HttpServletResponse response){
		
		//IFileUploadAction uploadAction = FileUploadActionFactory.getServletFileUploadAction();//测试不通过
		//IFileUploadAction uploadAction = FileUploadActionFactory.getSmartFileUploadAction();//测试不通过
		IFileUploadAction uploadAction = FileUploadActionFactory.getStrutsFileUploadAction();
		IFileDownloadAction downloadAction = new FileDownloadAction();
		
		Map paraMap = new HashMap();
		paraMap.put("form", actionform);
		
		try {
			String action = request.getParameter("action");
			if("doDownloadAttach".equals(action)){//下载文件
				String fileName = request.getParameter("fileName");
				String filePath = request.getParameter("filePath");
				downloadAction.doDownload(this.getServlet().getServletContext(), request, response, filePath, fileName);
			}if("doDeleteAttach".equals(action)){//删除文件
				String filePath = request.getParameter("filePath");
				uploadAction.doDelete(this.getServlet().getServletContext(), filePath);
			}else{//上传文件
				uploadAction.setParameter(paraMap);
				uploadAction.doUpload(this.getServlet().getServletConfig(), request, response);
				uploadAction.destroy();
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return actionmapping.findForward("fileUploadPage");
	}
}
